//
//  MoneyViewController.m
//  ShangAPP
//
//  Created by 徐番茄丶 on 16/1/22.
//  Copyright © 2016年 wangdao. All rights reserved.
//

#import "MoneyViewController.h"
#import "MyTools.h"
#import "MyMoneyTableViewCell.h"
#import "GetMoneyViewController.h"
#import "AccountViewController.h"
#import "ListViewController.h"
@interface MoneyViewController ()
<UITableViewDataSource,
UITableViewDelegate,
UIActionSheetDelegate>{
    UITableView *_myTab;
    NSDictionary *_mydic;
    
    NSString *_aliStr;
    NSString *_weiStr;
}

@end

@implementation MoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的钱包";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self.navigationItem.leftBarButtonItem setTintColor:UIColorFromRGB(0x444444)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-liebiao"] style:UIBarButtonItemStylePlain target:self action:@selector(list)];
    [self.navigationItem.rightBarButtonItem setTintColor:UIColorFromRGB(0x444444)];
    
    self.view.backgroundColor = UIColorFromRGB(0xefeff1);
    [self viewCreate];
}

-(void)getDate{
    NSString *url = @"http://www.xsy777.com/app/merchant/userinfolist.html";
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = @{@"user_token":[user objectForKey:@"token"]};
    
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [manager POST:url parameters:dic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            _mydic = [[NSDictionary alloc]initWithDictionary:[responseObject objectForKey:@"data"]];
            _aliStr = [NSString stringWithFormat:@"%@",[_mydic objectForKey:@"alipay"]];
            _weiStr = [NSString stringWithFormat:@"%@",[_mydic objectForKey:@"weixin"]];
            [_myTab reloadData];
//            NSLog(@"%@",_mydic);
        }else{
            [self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
    }];
    
}

-(void)viewCreate{
    _myTab = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    _myTab.rowHeight = 50;
    _myTab.backgroundColor = UIColorFromRGB(0xefeff1);
    _myTab.separatorStyle = UITableViewCellSeparatorStyleNone;
    _myTab.showsVerticalScrollIndicator = NO;
    _myTab.scrollEnabled = NO;
    _myTab.delegate = self;
    _myTab.dataSource = self;
    [self.view addSubview:_myTab];
}

#pragma mark tableview代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 2;
    }else if (section == 1){
        return 1;
    }
    return 0;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    MyMoneyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[MyMoneyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.titleLab.text = @"我的余额";
            cell.moneyLab.text = [NSString stringWithFormat:@"%@",[_mydic objectForKey:@"score1"]];
            cell.myImage.image = [UIImage imageNamed:@"我的余额"];
            UIView *footview = [[UIView alloc]initWithFrame:CGRectMake(0, 49, WIDTH, 0.6)];
            footview.backgroundColor = UIColorFromRGB(0xE0E0E0);
            [cell addSubview:footview];
        }else{
            cell.titleLab.text = @"我的积分";
            cell.moneyLab.text = [NSString stringWithFormat:@"%@",[_mydic objectForKey:@"score2"]];
            cell.myImage.image = [UIImage imageNamed:@"积分"];
        }
    }else{
        
        cell.titleLab.text = @"绑定账户";
        cell.myImage.image = [UIImage imageNamed:@"绑定账户"];
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        UIActionSheet * action = [[UIActionSheet alloc]initWithTitle:@"提现到" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"支付宝",@"微信", nil];
        action.tag = indexPath.row;
        [action showInView:self.view];
    }else if (indexPath.section == 1){
        AccountViewController *account = [[AccountViewController alloc]init];
        [self.navigationController pushViewController:account animated:YES];
    }
}




#pragma mark 选择提现账户
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        //支付宝
        if (_aliStr.length == 0) {
            [self.view makeToast:@"您还没有绑定支付宝，请先绑定支付宝账号" duration:0.8 position:CSToastPositionCenter];
        }else{
            GetMoneyViewController *get = [[GetMoneyViewController alloc]init];
            get.alistr = _aliStr;
            get.weistr = _weiStr;
            get.isHow = @"支付宝";
            get.isWhere = (int)actionSheet.tag;
            get.isDuiHuan = [[_mydic objectForKey:@"duihuan"] intValue];
            get.moneyStr = [NSString stringWithFormat:@"%@",[_mydic objectForKey:@"score1"]];
            get.fenStr = [NSString stringWithFormat:@"%@",[_mydic objectForKey:@"score2"]];
            [self.navigationController pushViewController:get animated:YES];
        }
    }else if (buttonIndex == 1){
        //微信
        if (_weiStr.length == 0) {
            [self.view makeToast:@"您还没有绑定微信，请先绑定微信账号" duration:0.8 position:CSToastPositionCenter];
        }else{
            GetMoneyViewController *get = [[GetMoneyViewController alloc]init];
            get.alistr = _aliStr;
            get.weistr = _weiStr;
            get.isWhere = (int)actionSheet.tag;
            get.isDuiHuan = [[_mydic objectForKey:@"duihuan"] intValue];
            get.moneyStr = [NSString stringWithFormat:@"%@",[_mydic objectForKey:@"score1"]];
            get.fenStr = [NSString stringWithFormat:@"%@",[_mydic objectForKey:@"score2"]];
            get.isHow = @"微信";
            [self.navigationController pushViewController:get animated:YES];
        }
    }
}


//提现记录
-(void)list{
    ListViewController *list = [[ListViewController alloc]init];
    [self.navigationController pushViewController:list animated:YES];
}
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.translucent = YES;
    self.tabBarController.tabBar.hidden = YES;
    [self getDate];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.translucent = NO;
    self.tabBarController.tabBar.hidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
